package com.android.duininsuoai.view.group;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.duininsuoai.R;
import com.android.duininsuoai.activity.BaseObject;
import com.android.duininsuoai.activity.BaseWeb;
import com.android.duininsuoai.activity.mine.BuyTicketActivity;
import com.android.duininsuoai.activity.mine.LoginActivity;
import com.android.duininsuoai.activity.mine.ScoreSignInActivity;
import com.android.duininsuoai.utils.ConstantsUrl;
import com.android.duininsuoai.utils.LoginState;
import com.android.duininsuoai.utils.ObjectUtils;


public class BannerItemFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    private String mParam1;
    private int mParam2;
    private String mParam3;
    private FragmentActivity mcontext;


    public static BannerItemFragment newInstance(String thumb, int photolength, String link) {
        BannerItemFragment fragment = new BannerItemFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, thumb);
        args.putInt(ARG_PARAM2, photolength);
        args.putString(ARG_PARAM3, link);
        fragment.setArguments(args);
        return fragment;
    }

    public BannerItemFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getInt(ARG_PARAM2);
            mParam3 = getArguments().getString(ARG_PARAM3);


        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_fragment_banner, null);
        ImageView iv = (ImageView) view.findViewById(R.id.imageView1);
        ObjectUtils.photo2(getActivity(), mParam1, iv);
        mcontext = getActivity();
        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(mParam3)){
                    jumpNext();
                }
            }
        });

        return view;
    }

    private void jumpNext() {
        String[] split = mParam3.split("/");
        String key = split[split.length-1];
        switch (key){
            // 签到
            case "sign":
                if (LoginState.getInstance().isLogin(mcontext)) {
                    Intent intent_q = new Intent(getActivity(), ScoreSignInActivity.class);
                    startActivity(intent_q);
                } else {
                    startActivity(new Intent(mcontext, LoginActivity.class));
                }
                break;
            // 购卷
            case "voucher":
                Intent intent_voucher = new Intent(mcontext,BuyTicketActivity.class);
                startActivity(intent_voucher);
                break;
            // 邀请好友
            case "invite":
                if (LoginState.getInstance().isLogin(mcontext)) {
                    ToObject("邀请好友");
                } else {
                    startActivity(new Intent(mcontext, LoginActivity.class));
                }
                break;
            default:
                if(mParam3.startsWith("http")){
                    toWeb();
                }
                break;
        }
    }

    private void toWeb() {
        Intent intent = new Intent(getActivity(), BaseWeb.class);
        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_WEB_URL, mParam3);
        startActivity(intent);
    }
    private void ToObject(String title) {
        Intent intent = new Intent(getActivity(), BaseObject.class);
        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, title);
        startActivity(intent);
    }

}
